package com.javalab.recorder.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.EventListener;
import java.util.List;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JPanel;

import com.javalab.recorder.event.OptionsDialogOKButtonClickEvent;
import com.javalab.recorder.listener.EventDispatcher;
import com.javalab.recorder.listener.OptionsDialogOKButtonClickEventListener;

public class OptionsDialog extends JDialog implements BaseUI {

	/**
	 *
	 */
	private static final long serialVersionUID = 1031725617085146880L;

	private JPanel contentPanel;

	private JPanel buttonPanel;

	private Window parent;

	private JComboBox audioInputDeviceComboBox;

	private JButton saveButton;

	private JButton cancelButton;

	private List<String> audioInputDeviceNameList = new ArrayList<String>();

	private EventDispatcher eventDispatcher = new EventDispatcher();

	public OptionsDialog() {
		contentPanel = new JPanel();
		buttonPanel = new JPanel();
		((FlowLayout) buttonPanel.getLayout()).setAlignment(FlowLayout.CENTER);
		add(contentPanel);
		add(buttonPanel, BorderLayout.SOUTH);

		audioInputDeviceComboBox = new JComboBox();
		contentPanel.add(audioInputDeviceComboBox);

		saveButton = initSaveButton();
		cancelButton = initCancelButton();

		buttonPanel.add(saveButton);
		buttonPanel.add(cancelButton);

		setTitle("Options...");
		setSize(300, 200);
		setLocation((Toolkit.getDefaultToolkit().getScreenSize().width - getSize().width) / 2, (Toolkit.getDefaultToolkit().getScreenSize().height - getSize().height) / 2);
		setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
	}

	/**
	 * Reset the content of ComboBox with current audioInputDeviceNameList property
	 */
	private void resetAudioInputDeviceComboBoxItems() {
		if (audioInputDeviceComboBox != null) {
			DefaultComboBoxModel model = (DefaultComboBoxModel) audioInputDeviceComboBox.getModel();
			model.removeAllElements();
			for (String name : audioInputDeviceNameList) {
				model.addElement(name);
			}
		}
	}

	/**
	 * Initialize a save button with action attached
	 * @return
	 */
	private JButton initSaveButton() {
		Action saveButtonAction = new AbstractAction() {
			private static final long serialVersionUID = 1L;

			@Override
			public void actionPerformed(ActionEvent paramActionEvent) {
				String selectedAudioInputDeviceName = (String) audioInputDeviceComboBox.getSelectedItem();
				OptionsDialogOKButtonClickEvent event = new OptionsDialogOKButtonClickEvent();
				event.setSelectedAudioInputDeviceName(selectedAudioInputDeviceName);
				for (OptionsDialogOKButtonClickEventListener listener : eventDispatcher.getListeners(OptionsDialogOKButtonClickEventListener.class)) {
					listener.handleActionEvent(event);
				}
				dispose();
			}
		};
		saveButtonAction.putValue(Action.NAME, "Save");
		JButton button = new JButton(saveButtonAction);
		return button;
	}

	/**
	 * Initialize a cancel button with action attached
	 * @return
	 */
	private JButton initCancelButton() {
		Action cancelButtonAction = new AbstractAction() {
			private static final long serialVersionUID = 1L;

			@Override
			public void actionPerformed(ActionEvent paramActionEvent) {
				dispose();
			}
		};
		cancelButtonAction.putValue(Action.NAME, "Cancel");
		JButton button = new JButton(cancelButtonAction);
		return button;
	}

	public void setAudioInputDeviceNameList(List<String> audioInputDeviceNameList) {
		this.audioInputDeviceNameList = audioInputDeviceNameList;
		resetAudioInputDeviceComboBoxItems();
	}

	public void setDefaultInputDeviceName(String defaultInputDeviceName) {
		audioInputDeviceComboBox.setSelectedItem(defaultInputDeviceName);
	}

	@Override
	public void subscribeEvent(EventListener eventListener) {
		eventDispatcher.subscribeEvent(eventListener);
	}

	@Override
	public void unsubscribeEvent(EventListener eventListener) {
		eventDispatcher.unsubscribeEvent(eventListener);
	}
}
